package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.SmsBiz;
import com.hst.capacity.application.biz.VehicleBiz;
import com.hst.capacity.domain.common.constants.MessageTemplate;
import com.hst.capacity.infra.external.sms.entinfo.SmsService;
import com.hst.capacity.infra.external.sms.entinfo.constants.SmsConstants;
import com.hst.capacity.infra.external.sms.entinfo.model.ToOneSMS;
import com.hst.capacity.infra.util.DateUtil;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SmsBizImpl implements SmsBiz {

    @Autowired
    SmsService smsService;

    @Autowired
    VehicleBiz vehicleBiz;

    @Override
    public void sendSmsMessage(String mobile, String context, String idMember) {
        Integer key = SmsConstants.capa.get(idMember);
        if (key == null)
            key = SmsConstants.key.MaDingShangLv;

        ToOneSMS sms = new ToOneSMS(mobile, context);
        smsService.sendSMSToSingle(sms, key);
    }

    @Override
    public void sendNewOrderSms(String mobile, LocalDateTime appointmentTime, int vehicleType, String idMember) {
        String vicheTypeName = vehicleBiz.findVehicleType(vehicleType);
        String context = String.format(MessageTemplate.BizTravelSms.NEW_ORDER_DISPATCH, DateUtil.formatDateTime(appointmentTime), vicheTypeName);
        this.sendSmsMessage(mobile, context, idMember);
    }

    @Override
    public void sendDispacthOrderSms(String mobile, LocalDateTime appointmentTime, int vehicleType, String flightNo, String passengerMobile, String idMember) {
        //给司机发短信
        String vicheTypeName = vehicleBiz.findVehicleType(vehicleType);
        String flightNoInfo = StringUtil.isNotBlank(flightNo) ? flightNo : "-";
        String context = String.format(MessageTemplate.BizTravelSms.ORDER_DISPATCH_TO_DRIVER, DateUtil.formatDateTime(appointmentTime), flightNoInfo, vicheTypeName, passengerMobile);
        this.sendSmsMessage(mobile, context, idMember);
    }

    @Override
    public void sendCancelOrderSms(String mobile, LocalDateTime appointmentTime, String idMember) {
        //给司机发短信
//        String vicheTypeName = vehicleBiz.findVehicleType(vehicleType);
//        String flightNoInfo = StringUtil.isNotBlank(flightNo) ? flightNo : "-";
        String context = String.format(MessageTemplate.BizTravelSms.ORDER_CANCEL_TO_DRIVER, DateUtil.formatDateTime(appointmentTime));
        this.sendSmsMessage(mobile, context, idMember);
    }

}
